/**  
* @Title: OrderSellController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年5月10日  
*/
package com.fortune.rainshine.service.controller;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.service.service.OrderService;
import com.fortune.rainshine.service.service.ShopService;
import com.fortune.rainshine.system.service.UserService;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.ShopModel;
import com.fortune.rainshinecommon.model.service.OrderDetailModel;
import com.fortune.rainshinecommon.model.service.OrderModel;
import com.fortune.rainshinecommon.model.system.User;
import com.google.gson.Gson;

/**  
* @ClassName: OrderSellController  
* @Description: TODO(售出订单管理)  
* @author 张鑫
* @phone 17561783855
* @date 2019年5月10日  
*    
*/
@Controller
@RequestMapping("/service/orderSell")
public class OrderSellController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
	@Autowired
	private OrderService service;
	
	@RequiresPermissions("orderSell:view")
	@RequestMapping()
	public String sellProduct(Model model, HttpServletRequest request) {
		//获取用户权限
		Set<String> authorities = getLoginUserAuthorities();
		//获取登录用户的按钮展示情况
		model.addAttribute("edit", authorities.contains("orderSell:detailForm"));
		model.addAttribute("confirming",authorities.contains("orderSell:confirming"));
		model.addAttribute("receiving", authorities.contains("orderSell:receiving"));
		return "order/orderSellList.html";
	}
	
	/**
	 * @Title: list
	 * @Description: 订单列表
	 * @author zhangxin
	 * @date 2019年3月26日
	 */
	@RequiresPermissions("orderSell:view")
	@ResponseBody
	@GetMapping("/list")
	public MsgModel sellList(Integer page, Integer limit, String beginDate, String endDate,Integer shopId) {
		try {
			if (page == null) {
				page = 0;
				limit = 0;
			}
			ParmsModel parm = new ParmsModel(beginDate,endDate,page,limit,shopId);
			parm.setType(2);//判断为获取销售列表
			return service.list(parm);
		} catch (Exception e) {
			logger.error("sellList ======> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}
	
	@RequiresPermissions("orderSell:detailForm")
	@RequestMapping("/detailForm")
	public String detailForm(String orderId,Model model) {
		List<OrderDetailModel> orders = service.findDetailOrders(orderId);
		model.addAttribute("orders",new Gson().toJson(orders));
		return "order/orderDetail.html";
	}
	
	@RequiresPermissions("orderSell:confirming")
	@ResponseBody
	@RequestMapping("/confirming")
	public MsgModel confirming(@RequestBody OrderModel model) {
		try {
			model.setUser(this.getLoginUser());
			return service.confirming(model);
		} catch (Exception e) {
			logger.error("confirming ======> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}
	
	/**
	 * @Title: cancelOrder
	 * @Description: 卖方取消订单
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月30日
	 */
	@RequiresPermissions("orderSell:cancel")
	@ResponseBody
	@RequestMapping("/cancel")
	public MsgModel cancelOrder(@RequestBody OrderModel model) {
		try {
			model.setUser(this.getLoginUser());
			return service.cancelOrder(model);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
	}

}
